package gameserver;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AuthServerMain implements Runnable{
	
    private int port=843;
    private ServerSocket serverSocket;
    private ExecutorService executorService;//线程池
    private final int POOL_SIZE=50;//单个CPU线程池大小
    private boolean serverStatus = false; //服务器状态
    String xml = "<?xml version=\"1.0\"?><cross-domain-policy><site-control permitted-cross-domain-policies=\"all\"/><allow-access-from domain=\"*\" to-ports=\"*\"/></cross-domain-policy>\0";
	 public void startServer()
     {

		Thread t = new Thread(this);
		t.start();
		 
     }
	 public void stopServer() {
		 serverStatus = true;
	}
	public void run(){
		 try {
			 System.out.println("授权服务器开始启动");
			 serverSocket=new ServerSocket(port);
			 executorService=Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*POOL_SIZE);
		     System.out.println("授权服务器启动完成");
    		 while(!serverStatus)
    		 {
    			 Socket socket=serverSocket.accept();//若客户机提请求，socket连接
 				 BufferedReader br = new BufferedReader(new InputStreamReader(socket
						.getInputStream(), "UTF-8"));
				 PrintWriter pw = new PrintWriter(socket.getOutputStream());
				 char[] by = new char[22];
				 br.read(by, 0, 22);
				 String s = new String(by);
				 System.out.println("s="+s);
				 if (s.equals("<policy-file-request/>")) {
					//System.out.println("接收policy-file-request");
					pw.print(xml);
					pw.flush();
					br.close();
					pw.close();
					socket.close();
				 } 
    		 }
		} catch (Exception e) {
			e.printStackTrace();
			
		}finally{
			 try {
				serverSocket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
